package cn.ultrarisk.scadmin.manager.web.controller.system;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.user.dao.PrivilegeDAO;
import cn.ultrarisk.scadmin.manager.biz.user.dao.RoleDAO;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Privilege;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Role;
import cn.ultrarisk.scadmin.manager.biz.user.service.RoleService;
import cn.ultrarisk.scadmin.manager.web.controller.BaseController;
import cn.ultrarisk.scadmin.manager.web.dto.system.RoleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * RoleController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping(value = "/system/role", produces = "application/json")
public class RoleController extends BaseController {
    @Autowired
    private RoleDAO roleDAO;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PrivilegeDAO privilegeDAO;

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<?> get(@PathVariable("id") Integer id) {
        Role role = roleDAO.get(id);
        return role == null ? getNotFoundResponseEntity() : getOkResponseEntity(role);
    }

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<Role>> list(String name, int pageNo, int pageSize) {
        Page<Role> roles = roleDAO.findPage(name, pageNo, pageSize);
        return getOkResponseEntity(roles);
    }

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(@RequestBody RoleDTO dto) {
        roleService.create(dto.getName(), dto.getPrivilegeIds());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public ResponseEntity<String> update(@PathVariable("id") Integer id, @RequestBody RoleDTO dto) {
        roleService.update(id, dto.getName(), dto.getPrivilegeIds());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(@PathVariable("id") Integer id) {
        roleService.delete(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/getByName", method = RequestMethod.GET)
    public ResponseEntity<Role> getByName(String name, Integer idNo) {
        return getOkResponseEntity(roleDAO.getByName(name, idNo));
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/privileges", method = RequestMethod.GET)
    public ResponseEntity<List<Privilege>> getPrivilegesByRoleId(@PathVariable("id") Integer roleId) {
        List<Privilege> privileges = privilegeDAO.findByRoleId(roleId);
        return getOkResponseEntity(privileges);
    }

    @ResponseBody
    @RequestMapping(value = "/privileges", method = RequestMethod.GET)
    public ResponseEntity<List<Privilege>> getPrivileges() {
        List<Privilege> privileges = privilegeDAO.assembly(privilegeDAO.findList(new Query()));
        return getOkResponseEntity(privileges);
    }
}
